package com.canbot.u05.activity.presidemode.newcode.view;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.canbot.u05.R;
import com.canbot.u05.activity.presidemode.newcode.bean.EDirection;
import com.canbot.u05.activity.presidemode.newcode.bean.MoveBean;
import com.canbot.u05.utils.ToastTools;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class NHostMoveSettingsDialog extends NHostBaseDialog {
    @BindView(R.id.et_input)
    EditText etInput;
    @BindView(R.id.radioGroup)
    RadioGroup radioGroup;
    @BindView(R.id.roundTip)
    TextView roundTip;
    @BindView(R.id.roundDanwei)
    TextView roundDanwei;
    private int mSpeed = MoveBean.SPEED_NORMAL;

    public NHostMoveSettingsDialog(Context context) {
        super(context);
        View view = LayoutInflater.from(this.getContext()).inflate(R.layout.nhost_move_settings_dialog, null);
        setContentView(view);
        ButterKnife.bind(this, view);
        setOnShowListener(v -> showKeyBoard(etInput));
        radioGroup.setOnCheckedChangeListener((group, checkedId) -> {
            switch (checkedId) {
                case R.id.lowRadio:
                    mSpeed = MoveBean.SPEED_LOW;
                    break;
                case R.id.normalRadio:
                    mSpeed = MoveBean.SPEED_NORMAL;
                    break;
                case R.id.highRadio:
                    mSpeed = MoveBean.SPEED_HIGH;
                    break;
            }
        });
    }

    private boolean isRotate = false;
    public void show(EDirection eDirection) {
        super.show();
        switch (eDirection) {
            case UP:
            case DOWN:
                roundTip.setText(getContext().getString(R.string.action_distance));
                roundDanwei.setText(getContext().getString(R.string.mi));
                break;
            case LEFT:
                isRotate = true;
                roundTip.setText(getContext().getString(R.string.round_angle_left));
                roundDanwei.setText(getContext().getString(R.string.angle));
                break;
            case RIGHT:
                isRotate = true;
                roundTip.setText(getContext().getString(R.string.round_angle_right));
                roundDanwei.setText(getContext().getString(R.string.angle));
                break;
        }
    }

    @OnClick({R.id.btn_cancel, R.id.btn_normal})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_cancel:
                dismiss();
                break;
            case R.id.btn_normal:
                String distanceOrAngle = etInput.getText().toString();
                if (TextUtils.isEmpty(distanceOrAngle)) {
                    if (isRotate){
                        ToastTools.newInstance(getContext()).showText(getContext().getString(R.string.angle_is_null));
                    }else {
                        ToastTools.newInstance(getContext()).showText(getContext().getString(R.string.distance_is_null));
                    }

                } else {
                    if (distanceOrAngle.startsWith(".")) {
                        if (isRotate){
                            ToastTools.newInstance(getContext()).showText(getContext().getString(R.string.anlge_is_invalid));
                        }else {
                            ToastTools.newInstance(getContext()).showText(getContext().getString(R.string.distance_is_invalid));
                        }

                    } else {
                        dismiss();
                        MoveBean moveBean = new MoveBean();
                        moveBean.distance = distanceOrAngle;
                        moveBean.speed = String.valueOf(mSpeed);
                        if (iDialogBtnLisenter != null) {
                            iDialogBtnLisenter.onConfirm(moveBean);
                        }
                    }
                }
                break;
        }
    }
}
